package com.example.demo.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * User: wcy
 * Date: 2017/4/8
 * Time: 15:46
 */
public enum AngelaAopLogger {

    INSTANCE;

    private static Set<String> RESULT_SUCCESS;

    private static String RESULT_CODE = "errCode";

    private static String RESULT_TIMEOUT = 9527+"";

    private static PropertiesConfiguration config;

    static{
        try {
            config = new PropertiesConfiguration("application.properties");
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        List a = new ArrayList();
        String success = config.getString("zchain.code.success");
        if(!StringUtils.isEmpty(success)){
            String[] codeArr = success.split(",");
            RESULT_SUCCESS = new HashSet<String>(Arrays.asList(codeArr));
        }
        String code = config.getString("zchain.code");
        RESULT_CODE = StringUtils.isEmpty(code)?RESULT_CODE:code;

    }

    public static void  loggerInterceptor(AngelaLoggerEntity logger){
        try{
            logger.setProject(LoggerThreadLocal.getProjectName());
            logger.setScenic(LoggerThreadLocal.getScenicName());
            logger.setPattern(AngelaType.LOGGER_PATTERN_INTERCEPTOR);
            logger = getLevel(logger);
            KafkaUtil.saveLogger(logger);
        }catch (Exception ex){
//            System.out.println(AngelaAopLogger.class+"日志保存失败");
        }

    }

    public static void updateLoggerInterceptor(String id, AngelaLoggerEntity logger){
        try{
            logger = getLevel(logger);
            KafkaUtil.updateLogger(id, logger);
        }catch (Exception ex){
//            System.out.println(AngelaAopLogger.class+"日志更新失败");
        }
    }

    public static void loggerHttpClient(AngelaLoggerEntity logger){
        try{
            logger.setUuid(LoggerThreadLocal.getNodeLoggerId());
            logger.setProject(LoggerThreadLocal.getProjectName());
            logger.setScenic(LoggerThreadLocal.getScenicName());
            logger.setPattern(AngelaType.LOGGER_PATTERN_HTTPCLIENT);
            logger = getLevel(logger);
            long s = System.currentTimeMillis();
            KafkaUtil.saveLogger(logger);
            long e = System.currentTimeMillis();
            System.out.println("kafka消耗"+(e-s));
            LoggerThreadLocal.setNodeId(logger.getUuid());
        }catch (Exception ex){
            //System.out.println(AngelaAopLogger.class+"loggerHttpClient日志保存失败");
        }
    }

    public static void debugger(AngelaLoggerEntity logger){
        String loggerType = AngelaType.LOGGER_TPPE_DEBUGGER;

        saveLoggerWithType(logger, loggerType);
    }

    public static void error(AngelaLoggerEntity logger){
        String loggerType = AngelaType.LOGGER_TPPE_ERROR;

        saveLoggerWithType(logger, loggerType);

    }

    public static void serverStartUp(AngelaLoggerProject logger) {
        KafkaUtil.saveLoggerProject(logger);
    }

    public static void serverShutDown(AngelaLoggerProject logger) {
        //TODO 关闭业务逻辑
        KafkaUtil.saveLoggerProject(logger);
    }

    private static void saveLoggerWithType(AngelaLoggerEntity logger, String loggerType) {
        try {
            logger.setType(loggerType);

            logger.setProject(LoggerThreadLocal.getProjectName());
            logger.setScenic(LoggerThreadLocal.getScenicName());
            logger.setPattern(AngelaType.LOGGER_PATTERN_LOGGER);
            KafkaUtil.saveLogger(logger);
        }catch (Exception ex){
//            System.out.println(AngelaAopLogger.class+"日志"+loggerType+"保存失败");
        }
    }

    private static AngelaLoggerEntity getLevel(AngelaLoggerEntity logger){
        int level =0;
        if(logger.getResult()!=null){
            try{
                JSONObject resultObj = JSON.parseObject(logger.getResult());
                String code = resultObj.getString(RESULT_CODE);
                logger.setStatus(code+"");
                if(RESULT_TIMEOUT.equals(code))
                    level =2;
                else if(!RESULT_SUCCESS.contains(code))
                    level =3;
                else if(code == null)
                    level =1;
            }catch (Exception ex){
                level =3;
                logger.setAlarmLevel(level);
                return logger;
            }
        }
        logger.setAlarmLevel(level);
        return logger;
    }

    public static void main(String[] args) {
    }
}
